//
//  WDCGViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/10.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//

#import "WDCGViewController.h"

@interface WDCGViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation WDCGViewController
{
    NSMutableArray *_listArray;
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的草稿";
    self.view.backgroundColor = BackColor;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -4, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"DSHTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    
    [self obtainTaskDataWithhud:YES];
    [self addRefresh];
}
- (void)addRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(obtain)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DSHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Tasklist *task = _listArray[indexPath.row];
    [cell.mainImgView sd_setImageWithURL:[NSURL URLWithString:task.t_img] placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.titleLabel.text = task.t_name;
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@元每单",task.money];
    cell.leftNumLabel.text = [NSString stringWithFormat:@"剩余%@个",task.num];
    cell.timeLabel.text = task.time;
    cell.stateLabel.hidden = YES;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 98;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[SQShowMessage shareMessage] showMessageWithStr:@"暂未开放"];
//    WebViewController *viewC = [[WebViewController alloc] init];
//    viewC.titleStr = @"预览详情";
//    viewC.url = [NSString stringWithFormat:@"http://www.vmijob.com/userlook/index/tdetail/task_id/%@",[_listArray[indexPath.row] t_id]];
//    [viewC setHidesBottomBarWhenPushed:YES];
//    [self.navigationController pushViewController:viewC animated:YES];
}
- (void)obtain
{
    [self obtainTaskDataWithhud:NO];
}
#pragma mark -------------- 待审核任务
- (void)obtainTaskDataWithhud:(BOOL)hud
{
    page = 1;
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"tasklist",@"uid":UID,@"type":@"5",@"page":@"1"} HUD:hud block:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            _listArray = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"result"]) {
                Tasklist *model = [[Tasklist alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
    }];
}
- (void)loadMoreTopics
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"tasklist",@"uid":UID,@"type":@"5",@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            for (NSDictionary *dic in responseObject[@"result"]) {
                Tasklist *model = [[Tasklist alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
